package com.xy.manage.action;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.manage.model.Tsurveyoptions;
import com.xy.manage.service.TsurveyoptionsService;
import com.xy.manage.service.TsurveyquestionService;



@Controller
public class TsurveyoptionsAction{
	private static final Logger log = LoggerFactory.getLogger(TsurveyoptionsAction.class);
	public TsurveyoptionsAction() {
	}
 
 
 	/**
	 * 问卷题目选项新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
//	@RequestMapping("v_tsurveyoptionsAdd.do")
//	public String add(HttpServletRequest request, ModelMap model) {
//		model.addAttribute("tsurveyoptions", new Tsurveyoptions());
//        setDict(model);
//		return "tsurveyoptionsEdit";
//	}
	
	@RequestMapping("v_tsurveyoptionsAdd.do")
	public String add(HttpServletRequest request, ModelMap model,long questionid) {
		Tsurveyoptions tsurveyoptions = new Tsurveyoptions();
		tsurveyoptions.setTsurveyquestion(tsurveyquestionService.get(questionid));
		model.addAttribute("tsurveyoptions", tsurveyoptions);
        setDict(model);
		return "tsurveyoptionsEdit";
	}

	/**
	 * 问卷题目选项新增保存
	 * 
	 * @param tsurveyoptions
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_tsurveyoptionsSave.do")
	public String save(Tsurveyoptions tsurveyoptions, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tsurveyoptions, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tsurveyoptions = tsurveyoptionsService.save(tsurveyoptions);
		return "redirect:v_tsurveyquestionDetail.do?questionid="+tsurveyoptions.getTsurveyquestion().getQuestionid();
	}

	/**
	 * 问卷题目选项删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tsurveyoptionsDelete.do")
	public String delete(long[] ids) {
		long qid = -1;
		if (ids != null) {
			for (long id : ids) {
				if(-1==qid)
					qid = tsurveyoptionsService.get(id).getTsurveyquestion().getQuestionid();
				tsurveyoptionsService.remove(id);
			}
		}
		if(-1!=qid)
			return "redirect:v_tsurveyquestionDetail.do?questionid="+qid;
		else
			return "redirect:v_tsurveyquestionList.do";
	}

	/**
	 * 问卷题目选项编辑页面
	 * 
	 * @param answerid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsurveyoptionsEdit.do")
	public String edit(long answerid, HttpServletRequest request, ModelMap model) {
		Tsurveyoptions tsurveyoptions = tsurveyoptionsService.get(answerid);
		model.addAttribute("tsurveyoptions", tsurveyoptions);
      model.addAttribute("actionEdit", request.getParameter("actionEdit"));
        setDict(model);
		return "tsurveyoptionsEdit";
	}

	/**
	 * 问卷题目选项详情
	 * 
	 * @param answerid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsurveyoptionsDetail.do")
	public String detail(long answerid, HttpServletRequest request, ModelMap model) {
		Tsurveyoptions tsurveyoptions = tsurveyoptionsService.get(answerid);
		model.addAttribute("tsurveyoptions", tsurveyoptions);
        setDict(model);
		return "tsurveyoptionsView";
	}

	/**
	 * 问卷题目选项查询（分页）
	 * 
	 * @param tsurveyoptions
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsurveyoptionsList.do")
	public String listPage(Tsurveyoptions tsurveyoptions, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tsurveyoptionsService.getTsurveyoptionsByPage(toMap(tsurveyoptions),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "tsurveyoptionsList";
	}

	/**
	 * 问卷题目选项查询
	 * 
	 * @param tsurveyoptions
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsurveyoptionsList1.do")
	public String list(Tsurveyoptions tsurveyoptions, HttpServletRequest request, ModelMap model) {
		List<Tsurveyoptions> list = tsurveyoptionsService.getTsurveyoptions(toMap(tsurveyoptions));
		model.addAttribute("list", list);
        setDict(model);
		return "tsurveyoptionsList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tsurveyoptions
	 * @return
	 */	private Map<String,Object> toMap(Tsurveyoptions tsurveyoptions){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tsurveyoptions.getAnswerid()!=null&&!tsurveyoptions.getAnswerid().equals("")){
			map.put("answerid", tsurveyoptions.getAnswerid());
		}
		if(tsurveyoptions.getTsurveyquestion()!=null&&!tsurveyoptions.getTsurveyquestion().equals("")){
			map.put("tsurveyquestion", tsurveyoptions.getTsurveyquestion());
		}
		if(tsurveyoptions.getOptionseq()!=null&&!tsurveyoptions.getOptionseq().equals("")){
			map.put("optionseq", tsurveyoptions.getOptionseq());
		}
		if(tsurveyoptions.getOptiondesc()!=null&&!tsurveyoptions.getOptiondesc().equals("")){
			map.put("optiondesc", tsurveyoptions.getOptiondesc());
		}
		if(tsurveyoptions.getBz()!=null&&!tsurveyoptions.getBz().equals("")){
			map.put("bz", tsurveyoptions.getBz());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tsurveyoptions tsurveyoptions, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   

	@Resource
	private TsurveyoptionsService tsurveyoptionsService;   
	@Resource
	private TsurveyquestionService tsurveyquestionService;   
}